<?php

//datenschutz-question-Datenbank - Fragen und Antworten auslesen
include_once('inc.php');

// Datenbank-Daten holen
//
// Get Categories
$categoryArray = array();
$selectAllCategoriesQuery = "SELECT * FROM category";
$resultCategory = mysql_query($selectAllCategoriesQuery,$conn) or die (mysql_error());
$num = mysql_num_rows($resultCategory);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultCategory);
	$tmpArrayCategory = array();
	$tmpArrayCategory['catnr'] = $row['catnr'];
	$tmpArrayCategory['catname'] = $row['catname'];
	$tmpArrayCategory['catmenu'] = $row['catmenu'];
	$categoryArray[$i] = $tmpArrayCategory;
}

// Get Questions
$questionsArray = array();
$selectAllQuestionsQuery = "SELECT * FROM questions";
$resultQuestion = mysql_query($selectAllQuestionsQuery,$conn) or die (mysql_error());
$num = mysql_num_rows($resultQuestion);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultQuestion);	
	$tmpArrayQuestion = array();
	$tmpArrayQuestion['questnr'] = $row['questnr'];
	$tmpArrayQuestion['quest'] = $row['quest'];
	$questionsArray[$i] = $tmpArrayQuestion;
}

// Get Answers
$answersArray = array();
$selectAllAnswers = "SELECT * FROM answers";
$resultAnswers = mysql_query($selectAllAnswers,$conn) or die (mysql_error());
$num = mysql_num_rows($resultAnswers);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultAnswers);
	$tmpArrayAnswer = array();
	$tmpArrayAnswer['questnr'] = $row['questnr'];
	$tmpArrayAnswer['answ'] = $row['answ'];
	$tmpArrayAnswer['pts'] = $row['pts'];
	$answersArray[$i] = $tmpArrayAnswer;
}

// Get QuestionCategory
$questionCategoryArray = array();
$selectAllQuestionCategory = "SELECT * FROM questioncategory";
$resultQuestionCategory = mysql_query($selectAllQuestionCategory,$conn) or die (mysql_error());
$num = mysql_num_rows($resultQuestionCategory);
for ($i = 0; $i < $num; $i++){
	$row = mysql_fetch_array($resultQuestionCategory);
	$tmpArrayQuestionCategory = array();
	$tmpArrayQuestionCategory['questnr'] = $row['questnr'];
	$tmpArrayQuestionCategory['catnr'] = $row['catnr'];
	$questionCategoryArray[$i] = $tmpArrayQuestionCategory;
}

function getCategoryName($catnr) 
{ 
	global $categoryArray;
    for($i = 0; $i < count( $categoryArray ); $i++ ) {
		if( $categoryArray[$i]['catnr'] == $catnr ) {
			return $categoryArray[$i]['catname'];
		}		
	}
} 

function getQuizID($index)
{
	global $questionCategoryArray;
	if($index < count($questionCategoryArray) )
	{
		$myIndex = $index +1;
		$resText = "quiz";
		$resText = $resText . $myIndex;
		$resText = $resText . "_" . getCategoryName( $questionCategoryArray[$index]['catnr'] );
		return $resText;
	}
	else {
		return "highscore";
	}
}

function getQuestionQuest($questnr)
{
	global $questionsArray;
	for($i = 0; $i < count( $questionsArray ); $i++ ) {
		if( $questionsArray[$i]['questnr'] == $questnr ) {
			return $questionsArray[$i]['quest'];
		}		
	}
}

function getAnswersAnswArray($questnr)
{
	global $answersArray;
	$answArray = array();
	$count = 0;
	for($i = 0; $i < count( $answersArray ); $i++ ) {
		if( $answersArray[$i]['questnr'] == $questnr ) {
			$answArray[$count++] = $answersArray[$i]['answ'];
		}		
	}
	return $answArray;
}
?>